package com.hjh.jpademo.repository;

import com.hjh.jpademo.entity.User;
import com.hjh.jpademo.vo.UserInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserRepository extends JpaRepository<User,Long>, JpaSpecificationExecutor<User> {
    public List<User> findByUsrName(String name);

    public List<User> findByUsrNameAndAndUsrPassword(String usrName, String usrPassword);

    public Long countByUsrName(String usrName);

    public List<User> findByUsrNameLike(String usrName);

    // /*原生sql*/
    @Query("select u from User u where u.usrRoleId=:roleId")
    public List<User> findByRoleId(@Param("roleId") Long roleId);


    @Transactional
    @Modifying
    @Query("update User set usrName=?1 where usrId = ?2")
    public int modifyNameById(String usrName, Long usrId);

    @Query("select u from User u where u.usrRoleId = ?1")
    public Page<User> findPageByUsrRoleId(Long roleId, Pageable pageable);

    @Query("select u.usrId as usrId, u.usrName as usrName, u.usrPassword as usrPassword, " +
            "u.usrRoleId as usrRoleId, u.usrFlag as usrFlag, r.roleName as roleName " +
            "from User  u,Role r where u.usrRoleId = r.roleId and u.usrId = ?1")
    public UserInfo getUserInfo(Long usrId);
}
